package com.mechat.util

import android.app.Activity
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.util.Log
import android.widget.ImageView

import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.mechat.R
import com.mechat.view.glide.listener.ProgressListener
import com.mechat.view.glide.util.BlurTransformation
import com.mechat.view.glide.util.ProgressInterceptor
import com.custom.base.manager.SDActivityManager



object GlideUtil {
    private const val TAG = "GlideUtil"
    /**
     * @param url
     * @param imageView
     * @param vagueness 是否模糊图片
     * @param radius 模糊半径
     * @param sampling 取样
     * @param errorImageId 加载失败图片
     * @param placeholderImageId 占位图片
     * @param cache 是否缓存
     */
    fun setImageView(
        url: String?,
        imageView: ImageView,
        vagueness:Boolean = false,
        radius:Int = 5,
        sampling:Int = 5,
        thumbnail:Float = 1f,
        errorImageId:Int = R.drawable.image_error,
        placeholderImageId:Int = R.mipmap.icon_image_loading,
        cache:Boolean = true,
        activity: Activity? = null
    ) {
        val mActivity:Activity = activity ?: SDActivityManager.instance.lastActivity
        val options = RequestOptions()
        options.error(errorImageId)
//        if(placeholderImageId!=0) options.placeholder(placeholderImageId)

        options.diskCacheStrategy(if(cache) DiskCacheStrategy.ALL else DiskCacheStrategy.NONE)

        if(vagueness) Glide.with(mActivity).load(url).apply(RequestOptions.bitmapTransform(
            BlurTransformation(radius,sampling)
        )).into(imageView)
        else Glide.with(imageView).load(url).apply(options).thumbnail(thumbnail).into(imageView)
    }
    fun setImageView(
        id: Int?,
        imageView: ImageView,
        vagueness:Boolean = false,
        radius:Int = 5,
        sampling:Int = 5,
        errorImageId:Int = R.drawable.ease_default_image,
        placeholderImageId:Int = R.drawable.icon_occupation_map,
        cache:Boolean = true,
        activity: Activity? = null
    ) {
        val mActivity:Activity = activity ?: SDActivityManager.instance.lastActivity
        val options = RequestOptions()
        options.error(errorImageId)
        options.placeholder(placeholderImageId)
        options.diskCacheStrategy(if(cache) DiskCacheStrategy.ALL else DiskCacheStrategy.NONE)
        if(vagueness) Glide.with(mActivity).load(id).apply(RequestOptions.bitmapTransform(
            BlurTransformation(radius,sampling)
        )).into(imageView)
        else Glide.with(mActivity).load(id).apply(options).into(imageView)
    }

    /**
     * 获取url BitMap
     */
    fun getImageBitMap(url: String,listener:OnGetBitMapListener? = null, activity: Activity? = null){
        val mActivity:Activity = activity ?: SDActivityManager.instance.lastActivity
        Glide.with(mActivity).asBitmap().load(url).into(object : SimpleTarget<Bitmap?>() {
            override fun onResourceReady(bitMap: Bitmap, p1: Transition<in Bitmap?>?) {
                listener?.onBitMap(bitMap)
            }
        })
    }

    fun setImageView(url: String, imageView: ImageView, listener: ProgressListener) {
        listener(url, listener)
        val simpleTarge = object : SimpleTarget<Drawable>() {
            override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                imageView.setImageDrawable(resource)
                Log.d(TAG, "onResourceReady: ")
                ProgressInterceptor.removeListener(url)
            }

            override fun onStart() {
                super.onStart()
                Log.d(TAG, "onStart: ")
            }
        }
        val options = RequestOptions()
        options.error(R.drawable.ease_default_image)
        options.placeholder(R.drawable.icon_occupation_map)
        options.diskCacheStrategy(DiskCacheStrategy.ALL)
        Glide.with(SDActivityManager.instance.lastActivity).load(url).apply(options)
            .into<SimpleTarget<Drawable>>(simpleTarge)
    }


    private fun listener(url: String, listener: ProgressListener) {
        ProgressInterceptor.addListener(url, listener)
    }

    interface OnGetBitMapListener{
        fun onBitMap(bitMap: Bitmap)
    }
}
